
package com.village.zpark.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Drug;
import com.village.zpark.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/drug")
public class DrugController {
    @Autowired
    private DrugService drugService;




    @RequestMapping
    public String login(){
        return "/drug";
    }
    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/drugPage")
    public String drugPage(){
        return "/drugPage";
    }
    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping(value = "/drugQueryById")
    public Object drugQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Drug drug = drugService.findById(id);
        model.addAttribute("obj",drug);
        return "/drugPage";
    }
    /**
     * 获取所有供应商
     * @return
     */
    @RequestMapping(value = "/drugList")
    @ResponseBody
    public Object drugList(){
        List<Drug> drugList = drugService.findAll();
        return ResultMapUtil.getHashMapList(drugList);
    }

    /**
     * 添加一个供应商
     * @return
     */
    @RequestMapping(value = "/drugAdd")
    @ResponseBody
    public Object drugAdd(Drug drug){
        try {
            drug.setProducttime(new Date());
            int i = drugService.addDrug(drug);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/drugEdit")
    @ResponseBody
    public Object drugEdit(Drug drug){
        try {
            int i = drugService.updateDrug(drug);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * @return
     */
    @RequestMapping(value = "/drugDelById")
    @ResponseBody
    public Object drugDelById(Integer id){
        try {
            int i = drugService.deleteDrugById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 分页查询供应商列表
     * @return
     */
    @RequestMapping(value = "/drugQueryPage")
    @ResponseBody
    public Map<String, Object> selectAllUer(String param, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drug> drugs = drugService.findPage(page, limit, param).getList();
        PageInfo<Drug> usersPageInfo = new PageInfo<>(drugs);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", usersPageInfo.getTotal());
        map.put("data", drugs);
        return map;
    }

//        try {
//            PageInfo<Drug> drugPageInfo = drugService.findPage(page, limit, param);
//            return ResultMapUtil.getHashMapMysqlPage(drugPageInfo);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultMapUtil.getHashMapException(e);
//        }
//    }

//        Map<String,Object>ret=new HashMap<String,Object>();
//        Map<String,Object>queryMap=new HashMap<String,Object>();
//        int id=(int) request.getSession().getAttribute("id");
//        queryMap.put("name",name);
//        queryMap.put("id",id);
//        queryMap.put("offset",page.g)


}